<?php
/**
 * Introduction: 活动逻辑
 * @author: 杨陈鹏
 * @email: yangchenpeng@cdlinglu.com
 */
namespace Common\Logic;

use Common\Model;

class ActivityLogic
{

    static public $model, $model_show, $model_and_member, $model_show_comment;

    public function __construct()
    {

        self::$model = new Model\ActivityModel();

        self::$model_show = new Model\ActivityShowModel();

        self::$model_and_member = new Model\ActivityAndMemberModel();

        self::$model_show_comment = new Model\ActivityShowCommentModel();

    }

    /**
     * Introduction: 获取列表数据
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function getData($where, $field = '*', $order = 'create_time desc', $page = 1, $nums = 20)
    {

        $data = self::$model
            ->where($where)
            ->field($field)
            ->order($order)
            ->page($page, $nums)
            ->select();

        $total = self::$model
            ->where($where)
            ->count();


        foreach ($data as $k => $v) {

            $data[$k]['create_time'] = date('Y-m-d H:i:s', $data[$k]['create_time']);
            $data[$k]['end_time'] = date('Y-m-d H:i:s', $data[$k]['end_time']);
            $data[$k]['images'] = $data[$k]['images'] ? unserialize($data[$k]['images']) : [];

        }

        return ['code' => 200, 'data' => $data,'total' => $total];


    }

    /**
     * Introduction: 修改活动
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function update($where,$data)
    {

        if(!$where)
            return ['code' => 300, 'msg' => '缺少条件!'];

        if (!$data['title'])
            return ['code' => 300, 'msg' => '标题不能为空!'];

        if (!$data['content'])
            return ['code' => 300, 'msg' => '内容不能为空!'];

        if (!$data['thumb'])
            return ['code' => 300, 'msg' => '请上传缩略图!'];

        if (!$data['end_time'])
            return ['code' => 300, 'msg' => '请选择结束时间!'];

        if (!$data['description'])
            return ['code' => 300, 'msg' => '活动描述不能为空!'];


        $datas['end_time'] = strtotime($data['end_time']);



        $datas['title'] = $data['title'];
        $datas['content'] = $data['content'];
        $datas['thumb'] = $data['thumb'];
        $datas['description'] = $data['description'];
        $datas['images'] = $data['images'] ? serialize($data['images']) : '';

        //return self::$model->fetchSql(true)->add($datas);

        $result = self::$model->where($where)->save($datas);

        if ($result !== false)
            return ['code' => 200, 'msg' => '更新成功!'];

        return ['code' => 300, 'msg' => '更新失败!'];


    }

    /**
     * Introduction: 活动详情
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function getOneData($activit_id, $fields = '*')
    {

        if (!intval($activit_id))
            return ['code' => 300, 'msg' => '缺少活动信息!'];

        $data = self::$model
            ->where(['id' => $activit_id])
            ->field($fields)
            ->find();

        $data['create_time'] = date('Y-m-d H:i:s', $data['create_time']);
        $data['end_time'] = date('Y-m-d H:i:s', $data['end_time']);
        $data['images'] = $data['images'] ? unserialize($data['images']) : [];

        return ['code' => 200, 'data' => $data];


    }

    /**
     * Introduction: 发布活动
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function create($data)
    {

        if (!$data['title'])
            return ['code' => 300, 'msg' => '标题不能为空!'];

        if (!$data['content'])
            return ['code' => 300, 'msg' => '内容不能为空!'];

        if (!$data['thumb'])
            return ['code' => 300, 'msg' => '请上传缩略图!'];

        if (!$data['end_time'])
            return ['code' => 300, 'msg' => '请选择结束时间!'];

        if (!$data['description'])
            return ['code' => 300, 'msg' => '活动描述不能为空!'];


        $datas['end_time'] = strtotime($data['end_time']);

        if ($datas['end_time'] < NOW_TIME)
            return ['code' => 300, 'msg' => '结束时间不能再当前时间之前'];


        $datas['title'] = $data['title'];
        $datas['content'] = $data['content'];
        $datas['thumb'] = $data['thumb'];
        $datas['description'] = $data['description'];
        $datas['create_time'] = NOW_TIME;
        $datas['images'] = $data['images'] ? serialize($data['images']) : '';

        //return self::$model->fetchSql(true)->add($datas);

        if (self::$model->add($datas))
            return ['code' => 200, 'msg' => '发布成功!'];

        return ['code' => 300, 'msg' => '发布失败!'];

    }

    static public function jionActivity($activity_id, $uid)
    {

        if (!$activity_id)
            return ['code' => 300, 'msg' => '缺少活动信息!'];

        if (!$uid)
            return ['code' => 500, 'msg' => '请登录!'];

        $data['activity_id'] = $activity_id;
        $data['uid'] = $uid;

        //检测是否加入如果加入则不再次加入
        if (self::$model_and_member->where($data)->count())
            return ['code' => 200, 'msg' => ''];

        if (self::$model_and_member->add($data)) {
            //加入成功,更新活动参与人数
            self::$model->where(['id' => $data['activity_id']])->setInc('persons');
            return ['code' => 200, 'msg' => '加入成功!'];
        }


        return ['code' => 300, 'msg' => '加入失败!'];


    }

    /**
     * Introduction: 插入秀图数据
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function addShow($data)
    {

        if (!intval($data['uid']))
            return ['code' => 500, 'msg' => '请登录!'];

        if (!intval($data['activity_id']))
            return ['code' => 300, 'msg' => '缺少活动信息!'];

        if (!$data['title'])
            return ['code' => 300, 'msg' => '标题不能为空!'];

        if (!$data['content'])
            return ['code' => 300, 'msg' => '内容不能为空!'];

        if (count($data['images']) < 1)
            return ['code' => 300, 'msg' => '请至少上传一张图片!'];

        $datas['uid'] = $data['uid'];
        $datas['activity_id'] = $data['activity_id'];
        $datas['content'] = $data['content'];
        $datas['images'] = serialize($data['images']);
        $datas['title'] = $data['content'];
        $datas['create_time'] = NOW_TIME;

        if (self::$model_show->add($datas))
            return ['code' => 200, 'msg' => '发布成功!'];

        return ['code' => 300, 'msg' => '发布失败!'];

    }
    /**
     * Introduction: 秀图详情
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function showDetail($show_id,$fields = '*')
    {

        if(!$show_id)
            return ['code' => 300, 'msg' => '缺少秀图信息'];

        $data = self::$model_show
                            ->where(['id' => $show_id])
                            ->field($fields)
                            ->find();

        $data['create_time'] = date($data['create_time']);
        $data['images'] = unserialize($data['images']);

        return ['code' => 200, 'data' => $data];



    }
    /**
     * Introduction: 秀图点赞
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function like($activity_show_id)
    {

        if (!$activity_show_id)
            return ['code' => 300, 'msg' => '缺少秀图信息!'];

        //return self::$model_show->fetchSql(true)->where(['id' => $activity_show_id])->setInc('likes');

        if (self::$model_show->where(['id' => $activity_show_id])->setInc('likes')) {

            $likes = self::$model_show->where(['id' => $activity_show_id])->getField('likes');
            return ['code' => 200, 'msg' => '操作成功!', 'data' => ['likes' => $likes]];

        }

        return ['code' => 300, 'msg' => '操作失败!'];


    }

    /**
     * Introduction: 秀图评论
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function showComment($data)
    {

        if (!$data['uid'])
            return ['code' => 500, 'msg' => '请登录!'];

        if (!intval($data['activity_show_id']))
            return ['code' => 300, 'msg' => '缺少秀图ID!'];

        if (!intval($data['activity_id']))
            return ['code' => 300, 'msg' => '请登录!'];

        if (!$data['content'])
            return ['code' => 300, 'msg' => '评论内容不能为空!'];

        if( self::$model_show_comment->where(['uid' => $data['uid']])->count() )
            return ['code' => 300, 'msg' => '您已对该修图评论过!'];

        $datas['uid'] = $data['uid'];
        $datas['activity_show_id'] = $data['activity_show_id'];
        $datas['activity_id'] = $data['activity_id'];
        $datas['content'] = $data['content'];
        $datas['create_time'] = NOW_TIME;

        if (self::$model_show_comment->add($datas)) {
            //评论成功,评论数量加一
            self::$model_show->where(['id' => $data['activity_show_id']])->setInc('comments');

            return ['code' => 200, 'msg' => '评论成功!'];

        }


        return ['code' => 300, 'msg' => '评论失败!'];

    }

    /**
     * Introduction: 秀图评论点赞
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function showCommentLike($show_comment_id)
    {

        if (!$show_comment_id)
            return ['code' => 300, 'msg' => '缺少修图评论信息!'];

        if (self::$model_show_comment->where(['id' => $show_comment_id])->setInc('likes')) {

            $likes = self::$model_show_comment->where(['id' => $show_comment_id])->getField('likes');

            return ['code' => 200, 'msg' => '操作成功!', 'data' => ['likes' => $likes]];

        }

        return ['code' => 300, 'msg' => '操作失败!'];

    }

    /**
     * Introduction: 修图列表
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function showLists($activity_id, $fields = '*', $order = 'create_time desc', $page = 1, $nums = 20, $is_images = false)
    {

        if (!$activity_id)
            return ['code' => 300, 'msg' => '缺少活动信息!'];

        if ($is_images)
            $fields = 'id,create_time,likes,images,uid,title';

        $data = self::$model_show
            ->where(['activity_id' => $activity_id])
            ->field($fields)
            ->order($order)
            ->page($page, $nums)
            ->select();

        $total = self::$model_show
            ->where(['activity_id' => $activity_id])
            ->count();

        if (!$data)
            return ['code' => 200, 'msg' => '此活动暂无秀图,赶紧加入吧!'];

        $member_logic = D('Common/Member', 'Logic');

        foreach ($data as $k => $v) {



            $member_info = $member_logic::$model
                ->where(['uid' => $v['uid']])
                ->field('username,nicname,mobile,avatar')
                ->find();


            $data[$k]['create_time'] = date('Y-m-d H:i:s', $data[$k]['create_time']);
            $data[$k]['images'] = unserialize($data[$k]['images']);


            if($member_info)
                $data[$k] = array_merge($data[$k], $member_info);

        }
        return ['code' => 200, 'data' => $data,'total' => $total];

    }

    /**
     * Introduction: 修图评论列表
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function showCommentLists($activity_show_id)
    {

        if (!$activity_show_id)
            return ['code' => 300, 'msg' => '缺少秀图信息!'];

        $data = self::$model_show_comment
            //->fetchSql(true)
            ->where(['activity_show_id' => $activity_show_id])
            ->field('*')
            ->order('create_time desc,likes desc')
            ->select();

        if (!$data)
            return ['code' => 200, 'msg' => '暂无评论!'];

        $member_logic = D('Common/Member', 'Logic');

        foreach ($data as $k => $v) {

            $data[$k]['create_time'] = date('Y-m-d H:i:s', $data[$k]['create_time']);

            $member_info = $member_logic::$model
                ->where(['uid' => $v['uid']])
                ->field('username,nicname,mobile,avatar')
                ->find();

            $data[$k] = array_merge($data[$k], $member_info);

        }

        return ['code' => 200, 'data' => $data];


    }
    /**
     * Introduction: 用户禅参与的活动列表
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    public function memberActivityList($data,$filed = '*',$page = 1, $nums = 20,$uid)
    {


        $do = $data['do'];


        $activity = self::$model_and_member
            ->where(['uid' => $uid])
            ->field('activity_id')
            ->select();

        if(!$activity)
            return ['code' => 200, 'msg' => '你还没有参与任何活动哦!'] ;

        $activity_ids = '';

        foreach($activity as $v)
        {

            $activity_ids .= $v['activity_id'].',';

        }

        $activity_ids = trim($activity_ids,',');

        $where = "id IN($activity_ids)";

        if($do == 'doing'){

            $where .= " AND end_time > ".NOW_TIME;

        } else if ($do == 'end'){

            $where .= " AND end_time < ".NOW_TIME;

        }

        $data = self::$model
            ->where($where)
            ->order('create_time desc')
            ->page($page,$nums)
            ->field($filed)
            ->select();

        foreach($data as $k => $v)
        {

            $data[$k]['create_time'] = date('Y-m-d H:i:s',$data[$k]['create_time']);
            $data[$k]['end_time'] = date('Y-m-d H:i:s',$data[$k]['end_time']);
            $data[$k]['images'] = $data[$k]['images'] ? unserialize($data[$k]['images']) : [] ;

        }

        return ['code' => 200 ,'data' => $data];




    }

}